package cn.edu.glut.user.controller;

import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.assertj.core.util.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sargeraswang.util.ExcelUtil.ExcelLogs;

import cn.edu.glut.dto.Response;
import cn.edu.glut.dto.TeacherInputDTO;
import cn.edu.glut.dto.UserIntro;
import cn.edu.glut.entity.Student;
import cn.edu.glut.entity.Teacher;
import cn.edu.glut.user.service.TeacherService;
import cn.edu.glut.user.service.UserDetailService;
import cn.edu.glut.utils.ExcelUtils;
import cn.edu.glut.vo.TeacherExcelVo;
import cn.edu.glut.vo.TeacherToAdminVO;
import lombok.Cleanup;
/**
 * 
 * @author Caizhfy
 * @version v.0.1
 * @createTime 2017年12月13日下午8:42:20
 * @email Caizhfy@163.com
 * @description 教师
 *
 */
@RestController
@RequestMapping("/teac")
public class TeacherController {
	@Autowired
	TeacherService teacherService;

	@RequestMapping("/new")
	public Response save(@Valid TeacherInputDTO teacherInputDTO) {
		return Response.success(teacherService.save(teacherInputDTO.convertToEntity()));
	}

	@RequestMapping("/new/upload")
	public Response save(@RequestParam("file") MultipartFile file) {
		if (file.isEmpty())
			return Response.success("文件不能为空");
		if (!ExcelUtils.isExcelFile(file.getOriginalFilename())) {
			return Response.success("请上传正确的Excel文件");
		}
		ExcelLogs logs = new ExcelLogs();
		try {
			Collection<Teacher> studentList = ExcelUtils.importExcel(TeacherExcelVo.class, Teacher.class,
					file.getInputStream(), null, 0, logs, null);
			if(null!=studentList && studentList.size()!=0)
				teacherService.save(studentList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Response.success();
	}

	@RequestMapping("/del/{id}")
	public Response remove(@PathVariable String id){
		teacherService.delete(id);
		return Response.success();
	}
	
	@RequestMapping("/dels")
	public Response remove(){
		teacherService.deleteAll();
		return Response.success();
	}
	
	@RequestMapping("/update")
	public Response update(@Valid TeacherInputDTO teacherInputDTO){
		Preconditions.checkNotNull(teacherInputDTO.getId(), "教师主键不能为空");//NPE
		return Response.success(teacherService.update(teacherInputDTO.convertToEntity()));
	}
	
	@RequestMapping("/get/{id}")
	public Response get(@PathVariable String id){
		return Response.success(teacherService.findById(id));
	}
	
	/**
	 * 
	 * @param pageable	分页数据
	 * @return
	 */
	@RequestMapping("/list")
	public Response list(@PageableDefault Pageable pageable, HttpSession session){
		UserIntro u = (UserIntro) session.getAttribute("user");
		if(u.getUserType() == UserDetailService.TYPE_STUDENT)
			return Response.success(teacherService.list(pageable, u.getId()));
		return Response.success(teacherService.list(pageable));
	}
	
	@RequestMapping("/export")
	public Response export(HttpServletResponse response) throws Exception{
		List<TeacherToAdminVO> voList = teacherService.list(null).getContent();
		if(voList==null || voList.size()==0)
			return Response.success("目前还没有数据");
		String fileName = "教师信息总览.xls";
		response.setHeader("Content-Disposition", "attachment;fileName=" + new String(fileName.getBytes("utf-8"),"iso-8859-1"));//解决乱码
		response.setContentType("application/octet-stream;charset=UTF-8");
		@Cleanup OutputStream out = response.getOutputStream();
		ExcelUtils.<TeacherToAdminVO>exportExcel(TeacherToAdminVO.getExcelCellKey(), voList, out);
		return Response.success();
	}
	
	@RequestMapping("/get/um/{username}")
	public Response getTeacherToAdminVOByUsername(@PathVariable String username){
		return Response.success(teacherService.findTeacherToAdminVOByUsername(username));
	}
}